package com.jack.submeiju.database;

import com.jack.submeiju.entity.TvStation;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class TvStationDb {
	
	public static void deleteTvStation(TvStation station) throws Exception{
		Connection conn = ConnectionManager.getSqlConnnection();
		if(conn == null){
			throw new Exception("jdbc connection is null!");
		}
		
		String sql = "update tvstation set deleted=1 where id=?";
		PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);
		ps.setInt(1, station.getId());
		
		ps.executeUpdate();
		ps.close();
		conn.close();
	}
	
	public static void updateTvStation(TvStation station) throws Exception{
		Connection conn = ConnectionManager.getSqlConnnection();
		if(conn == null){
			throw new Exception("jdbc connection is null!");
		}
		String sql = "update tvstation set name_ch=?,name_en=?,summary=?,picpath=?,ispublic=? where id=?";
		PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);
		ps.setString(1, station.getName_ch());
		ps.setString(2, station.getName_en());
		ps.setString(3, station.getSummary());
		ps.setString(4, station.getPicPath());
		ps.setBoolean(5, station.isPublic());
		ps.setInt(6, station.getId());
		ps.executeUpdate();
		ps.close();
		conn.close();
	}

	public static void addTvStation(TvStation station) throws Exception{
		Connection conn = ConnectionManager.getSqlConnnection();
		if(conn == null){
			throw new Exception("jdbc connection is null!");
		}
		String sql = "insert into tvstation(name_ch,name_en,summary,picpath,ispublic) values(?,?,?,?,?)";
		PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);
		ps.setString(1, station.getName_ch());
		ps.setString(2, station.getName_en());
		ps.setString(3, station.getSummary());
		ps.setString(4, station.getPicPath());
		ps.setBoolean(5, station.isPublic());
		int result = ps.executeUpdate();
		ps.close();
		conn.close();
		if(result <= 0){
			throw new Exception("error occurs on insert a TvStation to db.");
		}
	}
	

}
